package com.commonhelper.util.decoration;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import androidx.annotation.ColorInt;
import androidx.recyclerview.widget.RecyclerView;

/**
 * RecyclerView的LinearLayoutManager为横向布局的分割线
 * ChildView设置边距
 */
public class LinearLayoutDecoration extends RecyclerView.ItemDecoration{
    private Paint mPaint;
    private int mDividerWidth;
    private int mOrientation;

    private boolean showTop=false;
    private boolean showBottom=false;
    private boolean showLeft=false;
    private boolean showRight=false;


    public LinearLayoutDecoration(@ColorInt int color, int mDividerWidth, int orientation) {
        this.mDividerWidth = mDividerWidth;
        mOrientation = orientation;
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(color);
        mPaint.setStyle(Paint.Style.FILL);
    }

    public void setPadding(boolean showTop,boolean showBottom,boolean showLeft,boolean showRight) {
        this.showTop=showTop;
        this.showBottom=showBottom;
        this.showLeft=showLeft;
        this.showRight=showRight;
    }

    /**
     * 确定divider的范围
     * 由于Divider也有长宽高，每一个Item需要向下或者向右偏移
     * @param outRect   边界
     * @param parent
     */
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int itemPosition = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();
        int childCount = parent.getAdapter().getItemCount();
        boolean isFirst  = (itemPosition==0);//是否是第一条数据
        boolean isLast  = (itemPosition==childCount-1);//是否是最后一条数据

        if (mOrientation == RecyclerView.VERTICAL) {
            //默认每一个item画top,最后一个item还需要bottom。
            int top=mDividerWidth;
            int bottom=0;
            int left=0;
            int right=0;

            //处理第一个item的top
            if (isFirst&&!showTop){
                top=0;
            }

            //处理最后一个item的bottom
            if (isLast&&showBottom){
                bottom=mDividerWidth;
            }

            //paddingLeft
            if (showLeft){
                left=mDividerWidth;
            }
            //paddingRight
            if (showRight){
                right=mDividerWidth;
            }
            //设定itemview的边距 paddingLeft paddingTop paddingRight paddingBottom
            outRect.set(left, top, right, bottom);
        } else {
            //默认每一个item画left,最后一个item还需要right。
            int top=0;
            int bottom=0;
            int left=mDividerWidth;
            int right=0;

            //处理第一个item的top
            if (isFirst&&!showLeft){
                left=0;
            }

            //处理最后一个item的bottom
            if (isLast&&showRight){
                right=mDividerWidth;
            }

            //paddingTop
            if (showTop){
                top=mDividerWidth;
            }
            //paddingBottom
            if (showBottom){
                bottom=mDividerWidth;
            }
            //设定itemview的边距 paddingLeft paddingTop paddingRight paddingBottom
            outRect.set(left, top, right, bottom);
        }
    }


    /**
     * 画divider
     * @param canvas
     * @param parent
     */
    @Override
    public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
        if (mOrientation == RecyclerView.VERTICAL) {
            drawVERTICAL(canvas,parent);
        }else{
            drawHORIZONTAL(canvas,parent);
        }
    }

    public void drawVERTICAL(Canvas canvas, RecyclerView parent){
        int childCount = parent.getChildCount();;
        int left=0;
        int right=0;
        int top=0;
        int bottom=0;
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            //获得child的布局信息
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            if (!showTop&&i==0){
                //不画上分割线
            }else{
                //画上分割线
                left=child.getLeft()-mDividerWidth;
                right=child.getRight()+mDividerWidth;
                bottom = child.getTop() + params.topMargin;
                top=bottom-mDividerWidth;
                if (mPaint != null) {
                    canvas.drawRect(left, top, right, bottom, mPaint);
                }
            }

            if (!showBottom&&i==childCount-1){
                //不画下分割线
            }else{
                //画下分割线
                left=child.getLeft()-mDividerWidth;
                right=child.getRight()+mDividerWidth;
                top = child.getBottom() + params.bottomMargin;
                bottom = top + mDividerWidth;
                if (mPaint != null) {
                    canvas.drawRect(left, top, right, bottom, mPaint);
                }
            }

            if (showLeft){
                //画左分割线
                left=child.getLeft()-mDividerWidth;
                right=child.getLeft();
                top = child.getTop();
                bottom = child.getBottom();
                if (mPaint != null) {
                    canvas.drawRect(left, top, right, bottom, mPaint);
                }
            }

            if (showRight){
                //画右分割线
                left=child.getRight();
                right=child.getRight()+mDividerWidth;
                top = child.getTop();
                bottom = child.getBottom();
                if (mPaint != null) {
                    canvas.drawRect(left, top, right, bottom, mPaint);
                }
            }
        }
    }

    public void drawHORIZONTAL(Canvas canvas, RecyclerView parent){
        int childCount = parent.getChildCount();
        int left=0;
        int right=0;
        int top=0;
        int bottom=0;
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            //获得child的布局信息
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            //画上分割线
            left=child.getLeft()-mDividerWidth;
            right=child.getRight()+mDividerWidth;
            bottom = child.getTop() + params.topMargin;
            top=bottom-mDividerWidth;
            if (mPaint != null) {
                canvas.drawRect(left, top, right, bottom, mPaint);
            }
            //画下分割线
            left=child.getLeft()-mDividerWidth;
            right=child.getRight()+mDividerWidth;
            top = child.getBottom() + params.bottomMargin;
            bottom = top + mDividerWidth;
            if (mPaint != null) {
                canvas.drawRect(left, top, right, bottom, mPaint);
            }
            //画左分割线
            left=child.getLeft()-mDividerWidth;
            right=child.getLeft();
            top = child.getTop();
            bottom = child.getBottom();
            if (mPaint != null) {
                canvas.drawRect(left, top, right, bottom, mPaint);
            }
            //画右分割线
            left=child.getRight();
            right=child.getRight()+mDividerWidth;
            top = child.getTop();
            bottom = child.getBottom();
            if (mPaint != null) {
                canvas.drawRect(left, top, right, bottom, mPaint);
            }
        }
    }
}